package com.hbnu.jiani.service;

import com.hbnu.jiani.dao.PackageDao;
import com.hbnu.jiani.dao.UserDao;
import com.hbnu.jiani.dao.impl.PackageDaoImpl;
import com.hbnu.jiani.dao.impl.UserDaoImpl;
import com.hbnu.jiani.entity.Package;

import java.util.*;

public class PackageService {
    private PackageDao packageDao = new PackageDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    private Scanner scanner = new Scanner(System.in);

    public void addPackage() {
        System.out.print("请输入快递单号: ");
        String idStr = scanner.nextLine().trim();
        int id;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            System.out.println("快递单号必须是整数！");
            return;
        }
        if (packageDao.existsById(id)) {
            System.out.println("错误：快递单号 " + id + " 已存在，请使用其他单号！");
            return;
        }
        System.out.print("请输入收件人用户名: ");
        String recipient = scanner.nextLine().trim();
        if (!userDao.findByUsername(recipient).isPresent()) {
            System.out.println("错误：收件人 \"" + recipient + "\" 不存在，请先注册！");
            return;
        }
        System.out.print("请输入取件码: ");
        String pickupCode = scanner.nextLine().trim();
        if (pickupCode.isEmpty()) {
            System.out.println("取件码不能为空！");
            return;
        }
        if (packageDao.existsByPickupCode(pickupCode)) {
            System.out.println("错误：取件码 \"" + pickupCode + "\" 已被使用，请更换其他取件码！");
            return;
        }
        Package pkg = new Package(id,recipient, pickupCode, "已发货");
        if (packageDao.addPackage(pkg)) {
            System.out.println("快递添加成功！单号: " + pkg.getId());
        } else {
            System.out.println("添加失败！");
        }

    }

    public void deletePackage() {
        System.out.print("请输入要删除的快递单号: ");
        String input = scanner.nextLine().trim();
        try {
            int id = Integer.parseInt(input);
            if (!packageDao.existsById(id)) {
                System.out.println("快递单号不存在！");
                return;
            }
            if (packageDao.deletePackageById(id)) {
                System.out.println("快递删除成功！");
            } else {
                System.out.println("删除失败！");
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入有效的数字单号！");
        }
    }

    public void updatePackage() {
        System.out.print("请输入要修改的快递单号: ");
        String input = scanner.nextLine().trim();
        try {
            int id = Integer.parseInt(input);
            if (!packageDao.existsById(id)) {
                System.out.println("快递单号不存在！");
                return;
            }
            System.out.print("请输入新的收件人: ");
            String recipient = scanner.nextLine().trim();
            if (!userDao.findByUsername(recipient).isPresent()) {
                System.out.println("收件人不存在！");
                return;
            }
            System.out.print("请输入新的取件码: ");
            String pickupCode = scanner.nextLine().trim();
            System.out.print("请输入新的状态（如：已发货、运输中、已签收）: ");
            String status = scanner.nextLine().trim();
            Package pkg = new Package(id,recipient, pickupCode, status);
            pkg.setId(id);
            if (packageDao.updatePackage(pkg)) {
                System.out.println("快递信息更新成功！");
            } else {
                System.out.println("更新失败！");
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入有效的数字单号！");
        }
    }

    public void listAllPackages() {
        List<Package> packages = packageDao.findAll();
        if (packages.isEmpty()) {
            System.out.println("暂无快递记录。");
        } else {
            System.out.println("\n=== 所有快递 ===");
            packages.forEach(System.out::println);
        }
    }

    public void listUserPackages(String username) {
        List<Package> packages = packageDao.findByRecipient(username);
        if (packages.isEmpty()) {
            System.out.println("您暂无快递。");
        } else {
            System.out.println("\n=== 您的快递 ===");
            packages.forEach(System.out::println);
        }
    }
}
